package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBankDO;
import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicBankMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBrokerageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicBrokerageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 佣金组 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicBrokerageServiceImpl extends ServiceImpl<BasicBrokerageMapper, BasicBrokerageDO> implements BasicBrokerageService {

    @Resource
    private BasicBrokerageMapper basicBrokerageMapper;

    @Override
    public Long createBasicBrokerage(BasicBrokerageSaveReqVO createReqVO) {
        // 插入
        BasicBrokerageDO basicBrokerage = BeanUtils.toBean(createReqVO, BasicBrokerageDO.class);
        validateUnique(basicBrokerage);
        basicBrokerageMapper.insert(basicBrokerage);
        // 返回
        return basicBrokerage.getId();
    }

    @Override
    public void updateBasicBrokerage(BasicBrokerageSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicBrokerageExists(updateReqVO.getId());
        // 更新
        BasicBrokerageDO updateObj = BeanUtils.toBean(updateReqVO, BasicBrokerageDO.class);
        validateUnique(updateObj);
        basicBrokerageMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicBrokerage(Long id) {
        // 校验存在
        validateBasicBrokerageExists(id);
        // 删除
        basicBrokerageMapper.deleteById(id);
    }

    private void validateBasicBrokerageExists(Long id) {
        if (basicBrokerageMapper.selectById(id) == null) {
            throw exception(BASIC_BROKERAGE_NOT_EXISTS);
        }
    }

    @VisibleForTesting
    void validateUnique(BasicBrokerageDO data) {
        long count = basicBrokerageMapper.selectCount(new LambdaQueryWrapper<BasicBrokerageDO>().eq(BasicBrokerageDO::getCode,data.getCode()).ne(data.getId()!=null,BasicBrokerageDO::getId,data.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("价格组编码已存在");
        }
    }


    @Override
    public BasicBrokerageDO getBasicBrokerage(Long id) {
        return basicBrokerageMapper.selectById(id);
    }

    @Override
    public PageResult<BasicBrokerageDO> getBasicBrokeragePage(BasicBrokeragePageReqVO pageReqVO) {
        return basicBrokerageMapper.selectPage(pageReqVO);
    }

}